package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.MedicalPolicy;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.MedicalPolicyAddRequest;
import cn.ujn.medicalbackend.request.MedicalPolicyDeleteRequest;
import cn.ujn.medicalbackend.request.MedicalPolicyUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ujn.medicalbackend.domain.MedicalPolicy;
import cn.ujn.medicalbackend.mapper.MedicalPolicyMapper;
import cn.ujn.medicalbackend.service.MedicalPolicyService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Drink
* @description 针对表【medical_policy】的数据库操作Service实现
* @createDate 2024-10-26 10:28:48
*/
@Service
public class MedicalPolicyServiceImpl extends ServiceImpl<MedicalPolicyMapper, MedicalPolicy>
    implements MedicalPolicyService{

    @Override
    public Page<MedicalPolicy> medicalPolicyPageTransfer(Page<MedicalPolicy> tempPage) {
        return tempPage;
    }

    @Override
    public long addMedicalPolicy(MedicalPolicyAddRequest medicalPolicyAddRequest, HttpServletRequest request) {
        MedicalPolicy medicalPolicy = new MedicalPolicy();
        BeanUtils.copyProperties(medicalPolicyAddRequest, medicalPolicy);

        String now = new Date().toString();
        medicalPolicy.setCreateTime(now);
        medicalPolicy.setUpdateTime(now);
        // 3.校验信息
        if (medicalPolicy.getTitle() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能为空");
        }
        boolean result;
        result = save(medicalPolicy);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存失败");
        }

        return medicalPolicy.getId();
    }

    @Override
    public boolean updateMedicalPolicy(MedicalPolicyUpdateRequest medicalPolicyUpdateRequest, HttpServletRequest request) {
        if (medicalPolicyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        MedicalPolicy medicalPolicy =getById(medicalPolicyUpdateRequest.getId());
        if(medicalPolicy==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "不存在，更新失败");
        }
        BeanUtils.copyProperties(medicalPolicyUpdateRequest, medicalPolicy);
        medicalPolicy.setUpdateTime(now.toString());

        boolean result =updateById(medicalPolicy);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteMedicalPolicy(MedicalPolicyDeleteRequest medicalPolicyDeleteRequest, HttpServletRequest request) {
        Long medicalPolicyId = medicalPolicyDeleteRequest.getId();

        if (medicalPolicyId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；不能为空");
        }
        QueryWrapper<MedicalPolicy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", medicalPolicyId);
        long count = count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该药品");
        }

        boolean result = removeById(medicalPolicyId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }

}




